package mobi.greenplate.client.handler;

import mobi.greenplate.client.converter.UserConverter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import mobi.greenplate.client.domain.User;
import mobi.greenplate.client.parser.DomainHandler;

/**
 *
 * @author dmakariev
 */
public class UserListHandler extends DomainHandler<List<User>> {

    private static final String USER_TAG = "user";
    private static final String USER_BODY = "//user-list/user";
    private final List<User> userList = new ArrayList<User>();
    private User currentUser;

    @Override
    public List<User> getEntity() {
        return Collections.unmodifiableList(userList);
    }

    @Override
    public void endTag(String parentPath, String tagName, String text) {
        if (parentPath.equalsIgnoreCase(USER_BODY)) {
            currentUser = UserConverter.endTag(currentUser, tagName, text);
        }
    }

    @Override
    public void startTag(String parentPath, String tagName) {
        if (tagName.equalsIgnoreCase(USER_TAG)) {
            currentUser = new User();
            userList.add(currentUser);
        }
    }
}
